package com.meida.module.bus.provider.service.impl;

import com.meida.common.mybatis.base.service.impl.BaseServiceImpl;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.mybatis.query.CriteriaSave;
import com.meida.common.security.OpenHelper;
import com.meida.module.bus.client.entity.BusReceiptDeliveryRecord;
import com.meida.module.bus.provider.api.util.ApiConstants;
import com.meida.module.bus.provider.api.util.PmsApiUtil;
import com.meida.module.bus.provider.mapper.BusReceiptDeliveryRecordMapper;
import com.meida.module.bus.provider.service.BusRecDeliReqConfRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 预留发货记录表接口实现类
 *
 * @author flyme
 * @date 2020-12-02
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BusRecDeliReqConfRecordServiceImpl extends BaseServiceImpl<BusReceiptDeliveryRecordMapper, BusReceiptDeliveryRecord> implements BusRecDeliReqConfRecordService {

    @Autowired
    private PmsApiUtil pmsApiUtil;

    @Override
    public ResultBody beforeAdd(CriteriaSave cs, BusReceiptDeliveryRecord brdr, EntityMap extra) {
        return ResultBody.ok();
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<BusReceiptDeliveryRecord> cq, BusReceiptDeliveryRecord brdr, EntityMap requestMap) {
        cq.notIn("syncStatus", ApiConstants.REC_DELIVERY_INT);
        cq.like(BusReceiptDeliveryRecord.class, "syncStatus");
        cq.like(BusReceiptDeliveryRecord.class, "projectCode");
        cq.like(BusReceiptDeliveryRecord.class, "projectName");
        cq.like(BusReceiptDeliveryRecord.class, "majorCode");
        cq.like(BusReceiptDeliveryRecord.class, "majorName");
        cq.like(BusReceiptDeliveryRecord.class, "packageNo");
        cq.like(BusReceiptDeliveryRecord.class, "packageName");
        cq.like(BusReceiptDeliveryRecord.class, "userName");
        cq.like(BusReceiptDeliveryRecord.class, "appTime");
        cq.like(BusReceiptDeliveryRecord.class, "receivedDate");
        cq.like(BusReceiptDeliveryRecord.class, "zid");
        cq.like(BusReceiptDeliveryRecord.class, "zitem");
        cq.like(BusReceiptDeliveryRecord.class, "budat");
        cq.like(BusReceiptDeliveryRecord.class, "bldat");
        cq.like(BusReceiptDeliveryRecord.class, "bktxt");
        cq.like(BusReceiptDeliveryRecord.class, "supplierCode");
        cq.like(BusReceiptDeliveryRecord.class, "supplierName");
        cq.like(BusReceiptDeliveryRecord.class, "reserveNo");
        cq.like(BusReceiptDeliveryRecord.class, "reserveRow");
        cq.like(BusReceiptDeliveryRecord.class, "lgort");
        cq.like(BusReceiptDeliveryRecord.class, "msgtyFromSap");
        cq.like(BusReceiptDeliveryRecord.class, "msgtxFromSap");
        cq.like(BusReceiptDeliveryRecord.class, "mblnrFromSap");
        cq.like(BusReceiptDeliveryRecord.class, "mblppoFromSap");
        cq.like(BusReceiptDeliveryRecord.class, "mjahrFromSap");
        List<String> majorCodes = pmsApiUtil.getAuthMajorCodes(OpenHelper.getUserId());
        if(null != majorCodes) {
            cq.in("majorCode", majorCodes);
        }
        List<String> projectCodeList = pmsApiUtil.getAuthProjectIds(OpenHelper.getUserId());
        if(null != projectCodeList) {
            cq.in("projectCode", projectCodeList);
        }
        return ResultBody.ok();
    }
}
